package com.clippick;

import android.app.Activity;
import android.app.Fragment;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
import co.kr.clippick.R;

import com.clippick.api.GetApi;
import com.clippick.clipboard.PollingService;
import com.clippick.clipboard.ShorcutNotificationService;
import com.clippick.common.GlobalValue;
import com.clippick.dbbusiness.DBBusiness;

public class BehindFragmentV4 extends android.support.v4.app.Fragment {
	Context mContext;
	NewMainActivity activity;
	private View view;
	private TextView emailTextView, starCountTextView;
	private RelativeLayout myClipLayer, starLayer, settingLayer;
	private Button notiSwitchButton, tourButton, inviteButton, feedbackButton,
			logoutButton;

	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		view = inflater.inflate(R.layout.behind_setting, container, false);
		mContext = getActivity(); 
		activity = (NewMainActivity) mContext;

		emailTextView = (TextView) view.findViewById(R.id.email);
		starCountTextView = (TextView) view.findViewById(R.id.star_count);

		myClipLayer = (RelativeLayout) view.findViewById(R.id.buttons_1);
		starLayer = (RelativeLayout) view.findViewById(R.id.buttons_2);
		settingLayer = (RelativeLayout) view.findViewById(R.id.buttons_3);

		notiSwitchButton = (Button) view.findViewById(R.id.noti_switch);
		tourButton = (Button) view.findViewById(R.id.tour_button);
		inviteButton = (Button) view.findViewById(R.id.invite_button);
		feedbackButton = (Button) view.findViewById(R.id.feedback_button);
		logoutButton = (Button) view.findViewById(R.id.logout_button);

		String userEmail = GlobalValue.getUserEmail(mContext);
		if (userEmail.equals("")) {
			userEmail = GlobalValue.getFacebookEmail(mContext);
		}
		emailTextView.setText(userEmail);

		myClipLayer.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				NewMainActivity.IS_STAR_VIEW = false;
				activity.showAbove();
				starLayer.setBackgroundColor(Color.TRANSPARENT);
				myClipLayer.setBackgroundColor(Color.parseColor("#1a82bb"));
			}
		});

		starLayer.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				NewMainActivity.IS_STAR_VIEW = true;
				activity.showAbove();
				starLayer.setBackgroundColor(Color.parseColor("#1a82bb"));
				myClipLayer.setBackgroundColor(Color.TRANSPARENT);
			}
		});

		changeButtonDrawable(R.id.noti_switch,
				GlobalValue.isShowNotification(mContext));

		notiSwitchButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				boolean prevValue = GlobalValue.isShowNotification(mContext);
				GlobalValue.setShowNotification(mContext, !prevValue);
				changeButtonDrawable(R.id.noti_switch, !prevValue);
				showNotification(!prevValue);
			}
		});

		logoutButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				// delete user info
				GlobalValue.clearAllPrefsValue(mContext);

				// // it will close dbs
				// DBBusiness.closeDbs();
				//
				// // make it null object
				// DBBusiness.clearDbs();

				// stop service
				Intent intent = new Intent(mContext, PollingService.class);
				mContext.stopService(intent);

				Intent notiIntent = new Intent(mContext,
						ShorcutNotificationService.class);
				mContext.stopService(notiIntent);

				intent = new Intent(mContext, NewIntroActivity.class);
				startActivity(intent);
				getActivity().finish();
				//BehindFragmentV4.this.finish();

			}
		});

		feedbackButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(mContext, NewFeedbackActivity.class);
				startActivity(intent);
				activity.showAbove();
			}
		});

		inviteButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				getTinyUrl();
			}
		});

		starLayer.setBackgroundColor(Color.TRANSPARENT);
		myClipLayer.setBackgroundColor(Color.parseColor("#1a82bb"));

		return view;
	}

	private void changeButtonDrawable(int layout, boolean value) {
		switch (layout) {
		case R.id.noti_switch:
			if (value) {
				notiSwitchButton.setBackgroundDrawable(mContext.getResources()
						.getDrawable(R.drawable.switch_on));
			} else {
				notiSwitchButton.setBackgroundDrawable(mContext.getResources()
						.getDrawable(R.drawable.switch_off));
			}
			break;

		case R.id.buttons_1:
			if (value) {

			} else {

			}

			break;

		case R.id.buttons_2:
			break;
		}
	}

	/* invite */
	public void getTinyUrl() {
		Runnable runnable = new Runnable() {
			public void run() {
				GetApi api = new GetApi(mContext);
				sendFriendUrl(api.inviteTinyUrlApi());
			}
		};

		Thread networkThread = new Thread(runnable);
		networkThread.start();
	}

	private void sendFriendUrl(String tinyUrl) {
		Intent intent = new Intent();
		intent.setAction(Intent.ACTION_SEND);
		intent.putExtra(Intent.EXTRA_TEXT,
				"you can copy paste anywhere like post-it!\n" + tinyUrl);
		intent.setType("text/plain");
		startActivity(Intent.createChooser(intent, "Share to"));
	}

	private void showNotification(boolean isChecked) {
		Intent intent = new Intent(mContext, ShorcutNotificationService.class);
		if (isChecked) {
			mContext.startService(intent);
		} else {
			mContext.stopService(intent);
		}
	}

	private void setStarValue() {
		String starCountValue = String.valueOf(GlobalValue
				.getStarCountValue(mContext));
		String maxStarValue = String.valueOf(GlobalValue
				.getMaxStarValue(mContext));

		starCountTextView.setText("(" + starCountValue + "/" + maxStarValue
				+ ")");
	}

	/**
	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		
		IntentFilter filterShowIndicator = new IntentFilter();
		filterShowIndicator.addAction("com.new.clippick.showIndicator");
		mContext.registerReceiver(starValueReceiver, filterShowIndicator);

		setStarValue();
	}
	*/
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		IntentFilter filterShowIndicator = new IntentFilter();
		filterShowIndicator.addAction("com.new.clippick.showIndicator");
		mContext.registerReceiver(starValueReceiver, filterShowIndicator);

		setStarValue();

		super.onActivityCreated(savedInstanceState);
	}
	

	public void onResume() {
		super.onResume();
	}

	public void onDestroy() {
		super.onDestroy();
		mContext.unregisterReceiver(starValueReceiver);
	}

	BroadcastReceiver starValueReceiver = new BroadcastReceiver() {
		@Override
		public void onReceive(Context context, Intent intent) {
			setStarValue();
		}
	};
}
